package by.dezhits.makeanote;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import by.dezhits.makeanote.content.contract.FoldersContract;
import by.dezhits.makeanote.content.contract.NotesContract;
import by.dezhits.makeanote.content.contract.NotesContract.NotesColumns;
import by.dezhits.makeanote.db.DBHelper.DBFlags;
import by.dezhits.makeanote.fragment.FoldersFragment;
import by.dezhits.makeanote.fragment.NotesFragment;

public class MainActivity extends FragmentActivity implements OnClickListener {
	private View mSetPasswordDialogView;
	private ImageButton mNewPasswordDialogButton;
	private EditText mNewPasswordDialogEditText;
	private AlertDialog mAlertDialog;

	private NotesFragment mNotesFragment;
	private Integer mCurrentFolderId;
	private String mCurrentFolder;
	private TextView mCurrentFolderTextView;
	private boolean mClosed;
	private final static String ALL_NOTES = "All notes";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mNotesFragment = (NotesFragment) getSupportFragmentManager()
				.findFragmentById(R.id.fragment_notes);
		mCurrentFolderId = null;
		mCurrentFolder = null;
		mClosed = false;
		mCurrentFolderTextView = (TextView) findViewById(R.id.tv_folder_name);
		mCurrentFolderTextView.setText(ALL_NOTES);

		mSetPasswordDialogView = getLayoutInflater().inflate(
				R.layout.view_dialog_set_password, null);
		mNewPasswordDialogButton = (ImageButton) mSetPasswordDialogView
				.findViewById(R.id.btn_dialog_new_password);
		mNewPasswordDialogButton.setOnClickListener(this);
		mNewPasswordDialogEditText = (EditText) mSetPasswordDialogView
				.findViewById(R.id.et_dialog_new_password);
		AlertDialog.Builder adb = new AlertDialog.Builder(this);
		adb.setView(mSetPasswordDialogView).setTitle("New password");
		mAlertDialog = adb.create();
	}

	public void addNote(View v) {
		Intent intent = new Intent(MainActivity.this, NoteActivity.class);
		intent.putExtra(getString(R.string.request_code), NoteActivity.NEW_NOTE);
		if (mClosed) {
			intent.putExtra(getString(R.string.closed), DBFlags.TRUE);
		} else {
			intent.putExtra(getString(R.string.closed), DBFlags.FALSE);
		}
		intent.putExtra(getString(R.string.folder), mCurrentFolder);
		startActivity(intent);
	}

	public void setPassword(View v) {
		if (mCurrentFolder != null) {
			mAlertDialog.show();
		} else {
			Toast.makeText(this, "Only folders can be secured",
					Toast.LENGTH_LONG).show();
		}
	}

	public void testMethod(View v) {
		new FoldersFragment(new FoldersFragment.FoldersCallback() {

			@Override
			public void onFolderChosen(String selection, Integer id,
					String folder, boolean closed) {
				mNotesFragment.resetList(selection);
				mCurrentFolder = folder;
				mCurrentFolderId = id;
				mClosed = closed;
				if (folder == null) {
					mCurrentFolderTextView.setText(ALL_NOTES);
				} else {
					mCurrentFolderTextView.setText(mCurrentFolder);
				}
			}
		}).show(getSupportFragmentManager(), null);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_dialog_new_password:
			String newPassword;
			if (TextUtils.isEmpty((newPassword = mNewPasswordDialogEditText
					.getText().toString()))) {
				newPassword = null;

			}
			ContentValues values = new ContentValues();
			values.put(getString(R.string.name), mCurrentFolder);
			values.put(getString(R.string.id), mCurrentFolderId);
			values.put(getString(R.string.pass), newPassword);
			getContentResolver().insert(
					FoldersContract.FoldersColumns.CONTENT_URI, values);
			String closed = null;
			if (mClosed && newPassword == null) {
				mClosed = false;
				closed = DBFlags.FALSE;
			} else if (!mClosed && newPassword != null) {
				mClosed = true;
				closed = DBFlags.TRUE;
			}
			if (closed != null) {
				final String closedToPut = closed;
				new Thread(new Runnable() {

					@Override
					public void run() {
						Cursor c = getContentResolver().query(
								NotesContract.NotesColumns.CONTENT_URI, null,
								NotesContract.NotesColumns.FOLDER + "='" + mCurrentFolder + "'", null, null);
						if (c != null) {
							c.moveToFirst();

							while (!c.isAfterLast()) {
								ContentValues values = new ContentValues();
								values.put(
										getString(R.string.id),
										c.getInt(c
												.getColumnIndex(NotesContract.NotesColumns.ID)));
								values.put(
										getString(R.string.title),
										c.getString(c
												.getColumnIndex(NotesContract.NotesColumns.TITLE)));
								values.put(
										getString(R.string.text),
										c.getString(c
												.getColumnIndex(NotesContract.NotesColumns.TEXT)));
								values.put(
										getString(R.string.date),
										c.getString(c
												.getColumnIndex(NotesContract.NotesColumns.DATE)));
								values.put(
										getString(R.string.folder),
										c.getString(c
												.getColumnIndex(NotesContract.NotesColumns.FOLDER)));
								values.put(getString(R.string.closed), closedToPut);
								values.put(getString(R.string.should_notify), false);
								getContentResolver().insert(NotesContract.NotesColumns.CONTENT_URI, values);
								c.moveToNext();
							}
						}

					}
				}).start();
			}
			mNewPasswordDialogEditText.setText(null);
			mAlertDialog.dismiss();
			break;
		}
	}

}
